//
//  LCAccLoginViewController.m
//  wan77h5
//
//  Created by jiukuyinyue on 2017/9/1.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCAccLoginViewController.h"
#import "LCLoginTextField.h"

@interface LCAccLoginViewController ()


@property (weak, nonatomic) UITextField *userTextField;
@property (weak, nonatomic) UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *findPasswordButton;

@end

@implementation LCAccLoginViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
 
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(239, 239, 239);
    self.title = @"登录";
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    _loginButton.enabled = NO;//禁用
    [self setupLogin];
    
    [_userTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}
#pragma mark - 初始化布局
-(void)setupLogin{
    LCLoginTextField *userTextField = [[LCLoginTextField alloc] init];
    userTextField.font = [UIFont systemFontOfSize:15];
    userTextField.backgroundColor = [UIColor whiteColor];
    userTextField.borderStyle = UITextBorderStyleNone;
    userTextField.layer.borderColor = RGBColor(255, 255, 255).CGColor;
    userTextField.layer.borderWidth = 1;
    userTextField.layer.cornerRadius = 5;
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    userTextField.placeholder = @"手机号";

    _userTextField = userTextField;
    [self.view insertSubview:userTextField atIndex:0];
    
    LCLoginTextField *passwordTextField = [[LCLoginTextField alloc] initWithFrame:CGRectMake(54, 300, 211, 30)];
    passwordTextField.font = [UIFont systemFontOfSize:15];
    passwordTextField.backgroundColor = [UIColor whiteColor];
    passwordTextField.borderStyle = UITextBorderStyleNone;
    passwordTextField.layer.borderColor = RGBColor(255, 255, 255).CGColor;
    passwordTextField.layer.borderWidth = 1;
    passwordTextField.layer.cornerRadius = 5;
    passwordTextField.placeholder = @"请输入密码";
    passwordTextField.secureTextEntry = YES;//输入*号显示
    
    _passwordTextField = passwordTextField;
    [self.view insertSubview:passwordTextField atIndex:0];
    
    
    [_userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(userTextField.mas_centerY);
        make.width.offset(22);
        make.left.offset(30);
    }];
    
    [userTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(104);
        make.left.equalTo(_userImageView.mas_left).offset(-8);
        make.right.offset(-30);
        
        make.height.offset(40);
    }];
    [_codeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userTextField.mas_bottom).offset(20);
        make.width.offset(25);
        make.left.offset(30);
    }];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_codeImageView.mas_left).offset(-6);
        make.right.offset(-30);
        make.centerY.equalTo(_codeImageView.mas_centerY);
        make.height.offset(40);
    }];
    
    
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordTextField.mas_bottom).offset(20);
        make.left.equalTo(_passwordTextField.mas_left).offset(0);
        make.right.equalTo(_passwordTextField.mas_right).offset(0);
        
        make.height.offset(35);
    }];
    
    [_zhuceBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_loginButton.mas_bottom).offset(20);
        make.left.equalTo(_passwordTextField.mas_left).offset(0);
        make.right.equalTo(_passwordTextField.mas_right).offset(0);
        
        make.height.offset(35);
    }];
    
    [_findPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_zhuceBut.mas_centerX);
        make.top.equalTo(_zhuceBut.mas_bottom).offset(30);
    }];
    
}

/**
 监听键盘是否 输入用户名 密码
 */
-(void)textFieldDidChange :(UITextField *)theTextField{
    if (_passwordTextField.text.length <= 16 && _passwordTextField.text.length >= 6){
        _loginButton.enabled = YES;
    }else{
        _loginButton.enabled = NO;
    }
    
}

#pragma mark -  切换账户密码登录  -
- (IBAction)clickMobilePasswordView:(UIButton *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"FindPwd" bundle:nil];
    UIViewController *controller = [sb instantiateInitialViewController];
    [self.navigationController pushViewController:controller animated:YES];
}

/**
 登陆判断
 */
#pragma mark -  登陆判断  -
- (IBAction)clickLoginButton:(UIButton *)sender {
    
    if([LSZSpecialPackaging isValidateMobile:_userTextField.text] == NO ){
        [SVProgressHUD showErrorWithStatus:@"请输入正确手机号"];
        return;
    }
    
    [_UserM hmacSHA256ShouQuanJiamiWithUserCode:@""];
    
    NSDictionary *input = @{@"mobile":[NSString stringWithFormat:@"%@", _userTextField.text],
                            @"password":[NSString stringWithFormat:@"%@", _passwordTextField.text]
                            };
    
    [LSZRequestData initregistrationSendVerificationCode:input url:NEW_LOGIN_URL :^(BOOL isOK, NSString * str, NSDictionary * dic) {
        if (isOK == YES) {
            [_UserM setUsercode:[NSString stringWithFormat:@"%@", dic[@"data"]]];
            [_UserM setMobileNum:_userTextField.text];
            if (![dic[@"data"] isEqualToString:@""]) {
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SVProgressHUD showSuccessWithStatus:dic[@"msg"]];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }];
                [[NSNotificationCenter defaultCenter] postNotificationName:LOGINSUCCESSFUL object:self userInfo:nil];
            }

        }else{
            [SVProgressHUD showErrorWithStatus:str];
        }
    }];
    
}
#pragma mark - 注册按钮的点击事件
- (IBAction)clickZhuCeBut:(id)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MobileRegistered" bundle:nil];
    UIViewController *controller = [sb instantiateInitialViewController];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


 


@end
